package com.allwees.support.payment.payby.builder;

import cn.hutool.core.io.FileUtil;
import com.allwees.core.common.context.SpringContextHolder;
import com.allwees.support.payment.model.TransferInfo;
import com.allwees.support.payment.payby.configuration.PaybyProperties;
import com.payby.gateway.openapi.model.ExternalMoney;
import com.payby.gateway.openapi.request.PlaceTransferToBankOrderRequest;
import com.payby.gateway.sdk.misc.util.RsaUtil;

import java.nio.charset.Charset;

public class PlaceTransferRequestBuilder {
    private static PaybyProperties paybyProperties = SpringContextHolder.getBean(PaybyProperties.class);

    public static PlaceTransferToBankOrderRequest build(TransferInfo transferInfo) throws Exception {
        PlaceTransferToBankOrderRequest request = new PlaceTransferToBankOrderRequest();
        request.setMerchantOrderNo(transferInfo.getMerchantOrderNo());
        String payByPubKey = FileUtil.readUtf8String(paybyProperties.getPayby_public_key_path());
        request.setHolderName(RsaUtil.encrypt(transferInfo.getUserName(), Charset.forName("UTF-8"), payByPubKey, 2048));
        request.setIban(RsaUtil.encrypt(transferInfo.getCardNo(), Charset.forName("UTF-8"), payByPubKey, 2048));
        request.setSwiftCode(transferInfo.getSwiftCode());
        request.setAmount(new ExternalMoney(transferInfo.getAmt(), "AED"));
        request.setMemo(transferInfo.getMemo());
        request.setNotifyUrl(paybyProperties.getTransferResultNotifyURL());

        return request;
    }
}
